<?php

// +----------------------------------------------------------------------
// | H1CMS © OpenSource CMS
// +----------------------------------------------------------------------
// | Copyright (c) 2014-2016 http://www.h1cms.com All rights reserved.
// | Copyright (c) 2014-2016 嘉兴领格信息技术有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Allen <allen@lg4.cn>
// +----------------------------------------------------------------------
use Lg\Http\Input;
use h1cms\model\Node;
use h1cms\model\NodeLanguage;
use h1cms\model\NodeCategory;


// +----------------------------------------------------------------------
// | 创建文章
// +----------------------------------------------------------------------
function create_node() {
    $node = Input::post('node', []);
    //选择的分类
    $node_category = Input::post('node_category', []);
    $languages = Input::post('language', []);
    $node['author'] = h1cms\system\Auth::user()->id;
    $node['created_at'] = time();
    $node['updated_at'] = time();
    //创建Node
    $node_model = Node::create($node);

    //多语言
    foreach ($languages as $language) {
        $language['node_id'] = $node_model->node_id;
        NodeLanguage::create($language);
    }

    //Node 分类
    foreach ($node_category as $category_id) {
        NodeCategory::create([
            'node_id' => $node_model->node_id,
            'category_id' => $category_id
        ]);
    }


    return redirect('/node/node.php')->maybe_redirect()->withFlash('success', '文章添加成功');
}

// +----------------------------------------------------------------------
// | 更新文章
// +----------------------------------------------------------------------
function update_node(){
    $node = Input::post('node', []);
    $node_id = intval(Input::get('id'));
    //选择的分类
    $node_category = Input::post('node_category', []);
    $languages = Input::post('language', []);    
    
    $model = Node::find($node_id);
    if(empty($model)){
        return redirect('/node/node.php')->maybe_redirect()->withFlash('error', '文章不存在');
    }
    $node['updated_at'] = time();
    NodeCategory::delete($node_id);
    NodeLanguage::delete($node_id);
        
    //多语言
    foreach ($languages as $language) {
        $language['node_id'] = $node_id;
        NodeLanguage::create($language);
    }
    
    //Node 分类
    foreach ($node_category as $category_id) {
        NodeCategory::create([
            'node_id' => $node_id,
            'category_id' => $category_id
        ]);
    }
    return redirect('/node/node.php')->maybe_redirect()->withFlash('success', '文章更新成功');
}

function delete_node(){
    $node_id = intval(Input::get('id'));
    Node::delete($node_id);
    NodeCategory::delete($node_id);
    NodeLanguage::delete($node_id);
    return redirect('/node/node.php')->maybe_redirect()->withFlash('success', '文章删除成功');
}
